package com.zhangchuchu.neohealth.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zhangchuchu.neohealth.entity.DTO.RecordsDTO;
import com.zhangchuchu.neohealth.entity.Records;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface RecordsMapper extends BaseMapper<Records> {
    /**
     * 根据传入的年份和月份统计该月的记录数
     *
     * @param year  年份
     * @param month 月份
     * @return 该月记录数
     */
    @Select("SELECT COUNT(*) FROM records " +
            "WHERE YEAR(datetime) = #{year} AND MONTH(datetime) = #{month} AND is_sign_in = 1 ")
    int countRecordsByMonth(@Param("year") Integer year, @Param("month") Integer month);

    /**
     * 根据user_id和当前年月查询records及用户信息
     *
     * @param userId 用户ID
     * @param year   年份
     * @param month  月份
     * @return 查询结果列表
     */
    @Select("SELECT r.id , r.content, r.datetime, up.nickname, up.avatar, up.area " +
            "FROM records r " +
            "JOIN user_profile up ON r.user_id = up.user_id " +
            "WHERE r.user_id = #{userId} " +
            "AND r.is_sign_in = 1 " +
            "AND YEAR(r.datetime) = #{year} " +
            "AND MONTH(r.datetime) = #{month}" )
    List<RecordsDTO> getRecordsWithUserProfile(@Param("userId")Integer userId, @Param("year")Integer year, @Param("month")Integer month);
}
